<?php
/**
 * 功能和<UserColumn.php>相同，当调用Kartik的GridView插件的时候，需要用此文件
 * User: ZFC
 * Date: 2016/1/7
 * Time: 14:18
 */

namespace common\widgets;

use kartik\grid\ColumnTrait;
use kartik\grid\GridView;
use yii;
use yii\helpers\Html;
use yii\helpers\Url;


class KartikUserColumn extends \yii\grid\DataColumn
{

    use ColumnTrait;//此项不能去，否则当表格里有汇总的时候，会出现偏移


    public $hidden;

    public $hiddenFromExport = false;

    public $hAlign = GridView::ALIGN_CENTER;

    public $vAlign = GridView::ALIGN_MIDDLE;

    public $noWrap = false;


    public $width = '100px';

    public $pageSummary = false;

    public $pageSummaryFunc = GridView::F_SUM;

    public $pageSummaryOptions = [];

    public $hidePageSummary = false;

    public $mergeHeader = false;

    public $format = 'html';

    public $xlFormat;


    private $_rows = [];

    /**
     * @inheritdoc
     */
    public function init()
    {
        $this->parseFormat();
        $this->parseVisibility();
        parent::init();
        $this->setPageRows();
    }

    /**
     * @inheritdoc
     */
    public function renderDataCell($model, $key, $index)
    {

        $user_id   = 0;
        $trade_id  = 0;
        $real_name = '';
        $out = '&nbsp;';
        if(isset($model->tradeID)){
            $user_id = $model->id;
            $trade_id = $model->tradeID;
        }elseif(!empty($model->user)){
            $user_id = $model->userID;
            $trade_id = $model->user->tradeID;
        }

        if($trade_id){
            $menu = Dropdown::widget([
                'items' => [
//                    ['label'=>'查看关系', 'url'=>Url::to(['transaction/relation', 'id'=>$user_id])],
                    ['label'=>'编辑基本资料', 'url'=>Url::to(['user/edit-base', 'id'=>$user_id])],
                    ['label'=>'交易记录', 'url'=>Url::to(['transaction/list-deal', 'id'=>$user_id])],
                    ['label'=>'委托记录', 'url'=>Url::to(['transaction/list-buy', 'id'=>$user_id])],
                    ['label'=>'出入金记录', 'url'=>Url::to(['transaction/list-charge', 'id'=>$user_id])],
//                    ['label'=>'加入黑名单', 'url'=>Url::to(['black-white/blacklist-add', 'userID'=>$user_id])],
//                    ['label'=>'加入白名单', 'url'=>Url::to(['black-white/whitelist-add', 'userID'=>$user_id])],
                ],
                'pullRight' => true,
            ]);
            if(!$real_name) $real_name = $user_id;
            $out = '<div class="btn-group">
                      <a href="'.Url::to(['user/profile', 'id'=>$user_id]).'" class="btn btn-xs btn-primary" data-toggle="dropdown" data-hover="dropdown" >'.$trade_id.'
                      <i class="fa fa-angle-down"></i></a>
                        '.$menu.'
                    </div>';
        }
        $options = $this->fetchContentOptions($model, $key, $index);
        $this->parseExcelFormats($options, $model, $key, $index);
        return Html::tag('td', $out, $options);
    }
}